001    package videoautomat.contentcreator;
002    import javax.swing.Box;
003    import javax.swing.BoxLayout;
004    import javax.swing.JComponent;
005    import javax.swing.JLabel;
006    import javax.swing.JPanel;
007    
008    import sale.FormSheet;
009    import sale.FormSheetContentCreator;
010    import sale.FormSheet.FormButton;
011    import videoautomat.SaleProcessRent;
012    import videoautomat.contentcreator.stdactions.TransitWithAction;
013    import videoautomat.transition.RentPayConfirmTransition;
014    import videoautomat.transition.RentPayRollbackTransition;
015    import data.CatalogItemValue;
016    import data.DataBasket;
017    import data.IntegerValue;
018    import data.NumberValue;
019    import data.events.StockChangeAdapter;
020    import data.events.StockChangeEvent;
021    import data.ooimpl.MoneyBagImpl;
022    /**
023     * Changes the FormSheet layout and adds some labels and a listener
024     * to change the pay button state.
025     *
026     * @author Tobias Ruch
027     */
028    public class RentPayFSContentCreator extends FormSheetContentCreator {   
029       /** String of the pay value */
030       private String payValue;
031       /** Process in which the FormSheet and so the content creator is used */
032       private SaleProcessRent processRent;
033       
034       /**
035        * Creates the content creator.
036        * @param process - <code>SaleProcessRent</code> in which the ContentCreator is use to have access to some
037        *                     process data 
038        */
039       public RentPayFSContentCreator(SaleProcessRent process){
040          this.processRent = process;
041          payValue = "";
042       }
043       
044       /**
045       * Changes the FormSheetContent of the gives <code>FormSheet</code>.
046       * Adds labels, button and there actions
047       * @param fs - a <code>FormSheet</code> which should be changed.
048       */
049       protected void createFormSheetContent(FormSheet fs) {      
050          JComponent jc = new JPanel();
051          jc.setLayout(new BoxLayout(jc, BoxLayout.Y_AXIS));
052          jc.add(Box.createVerticalStrut(10));
053          jc.add(new JLabel("You have to pay: " + payValue));
054          jc.add(Box.createVerticalStrut(10));
055          jc.add(fs.getComponent());
056          
057          fs.setComponent(jc);
058          fs.removeAllButtons();
059            
060          fs.addButton("Pay", 1, new TransitWithAction(new RentPayConfirmTransition()));
061    
062          fs.addButton("Cancel", 2, new TransitWithAction(new RentPayRollbackTransition()));
063          fs.getButton(1).setEnabled(false);
064          setButtonStockListener(fs.getButton(1));
065       }    
066    
067       
068       public void setPayValue(String payValue){
069          this.payValue = payValue;
070       }   
071       /**
072         * Adds an implementation of the <code>StockChangeListener</code> to the given button, so that this button gets
073         * enabled if there is enough money in the temporar-moneybag and otherwise it gets disabled
074         * 
075         * @param fb
076         *                  the <code>FormButton</code> the listener should en/disable
077         */
078       private void setButtonStockListener(final FormButton fb) {
079          final MoneyBagImpl bag = (MoneyBagImpl) processRent.getContext().getProcessData(SaleProcessRent.MB_TEMP_KEY);
080          final DataBasket basket = processRent.getBasket();
081          final NumberValue nv_sum = (NumberValue) processRent.getContext().getProcessData(SaleProcessRent.SUM_KEY);
082          
083          //new StockChangeApdater, which is extended by an anonymous class 
084          //which overrides the addedStockItems method
085          
086          StockChangeAdapter sca = new StockChangeAdapter() {
087             public void addedStockItems(StockChangeEvent e) {
088                
089                
090                if (bag.sumStock(basket, new CatalogItemValue(), new IntegerValue(0))
091                      .compareTo(nv_sum)>= 0){
092                   fb.setEnabled(true);
093                }
094             }
095             public void removedStockItems(StockChangeEvent e) {
096                if (bag.sumStock(basket, new CatalogItemValue(), new IntegerValue(0))
097                      .compareTo(nv_sum) < 0){
098                   fb.setEnabled(false);
099                }
100             }
101          };
102          
103          bag.addStockChangeListener(sca);
104       }  
105    }